library(tidyverse)
library(ggplot2)

cdr <- read_csv('/Users/celinascott-buechler/DFP/cdr/cdr_cleaned.csv', col_names =TRUE)

####START OF FIGURES####

cdr$CommunityEngagement <- factor(
  cdr$CommunityEngagement,
  levels = c(
    "No requirement about community engagement, benefit, or ownership between the community and CDR developers",
    "Requiring that CDR developers consult with the community where they site a project",
    "Requiring that CDR developers consult with and invest in the community where they site a project",
    "Requiring that CDR developers allow the community where they site a project to have voting stakes in the project",
    "Requiring that CDR developers be paid for their construction of the project, but that the community where they site a project ultimately owns, operates, and profits from the project"),
  labels = c(
    "None",
    "Consult community",
    "Consult and invest in community",
    "Community gets voting stakes",
    "Community owns project")
  )

ggplot(data=cdr, aes(x=CommunityEngagement, fill=CommunityEngagement)) +
  geom_bar()+
  scale_x_discrete(labels = function(x) str_wrap(x, width = 20))+
  theme(text = element_text(size=14))+
  xlab("Requirements for community engagement")+
  ylab("Count")+
  theme(legend.position = "none")

#ff_cdr_transform <- cdr %>% pivot_longer(cols=c())

table(cdr$CommunityEngagement)/length(cdr$CommunityEngagement)*100

cdr_gg <- cdr %>% dplyr::select(FossilFuelRole_Nationalization, FossilFuelRole_Untrustworthy, FossilFuelRole_Experience, FossilFuelRole_ProvideEnergy) %>% pivot_longer(cols = c(FossilFuelRole_Nationalization, FossilFuelRole_Untrustworthy, FossilFuelRole_Experience, FossilFuelRole_ProvideEnergy),names_to = "category", values_to = "responses")

cdr_gg$category[cdr_gg$category == 'FossilFuelRole_Nationalization'] <- "Should nationalize"
cdr_gg$category[cdr_gg$category == 'FossilFuelRole_Untrustworthy'] <- "Is untrustworthy"
cdr_gg$category[cdr_gg$category == 'FossilFuelRole_Experience'] <- "Brings experience"
cdr_gg$category[cdr_gg$category == 'FossilFuelRole_ProvideEnergy'] <- "Provided energy"

cdr_gg_count <- as.data.table(cdr_gg)
cdr_gg_count$responses <- factor(cdr_gg_count$responses,
                                 levels = c("Strongly disapprove",
                                            "Somewhat disapprove",
                                            "Somewhat approve",
                                            "Strongly approve",
                                            "Haven’t heard enough to say"))

cdr_gg$responses <- factor(cdr_gg$responses,
                           levels = c("Strongly disapprove",
                                      "Somewhat disapprove",
                                      "Somewhat approve",
                                      "Strongly approve",
                                      "Haven’t heard enough to say"))

unique(cdr$EconBenefits)

ggplot(data=cdr_gg, aes(x=responses, y=..count.., fill=category)) +
  geom_bar(position="dodge", stat="count") +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 20)) +
  theme(text = element_text(size=13.75)) +
  xlab("Role for Fossil Fuel Companies") +
  ylab("Count") +
  scale_fill_brewer(palette="Set1")

cdr$EconBenefits <- factor(cdr$EconBenefits, level=c("Greatly worsen","Slightly worsen", "No effect","Slightly improve", "Greatly improve", "Don’t know"))

ggplot(data=cdr, aes(x=EconBenefits, y=..count.., fill=EconBenefits)) +
  geom_bar(position="dodge", stat="count") +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 40)) +
  theme(text = element_text(size=13.75)) +
  xlab("Percieved impact on economy") +
  ylab("Count") +
  scale_fill_brewer(palette="RdYlGn")

table(cdr$EconBenefits)/length(cdr$EconBenefits)*100

cdr$CarbonPollution <- factor(cdr$CarbonPollution, level=c("Greatly increase","Somewhat increase", "No effect","Somewhat decrease", "Greatly decrease", "Don’t know"))

ggplot(data=cdr, aes(x=CarbonPollution, y=..count.., fill=CarbonPollution)) +
  geom_bar(position="dodge", stat="count") +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 40)) +
  theme(text = element_text(size=13.75)) +
  xlab("Percieved impact on carbon dioxide levels") +
  ylab("Count") +
  scale_fill_brewer(palette="RdYlGn")

cdr$RenewablesUse <- factor(cdr$RenewablesUse, level=c("Greatly decrease","Somewhat decrease", "No effect","Somewhat increase", "Greatly increase", "Don’t know"))

ggplot(data=cdr, aes(x=RenewablesUse, y=..count.., fill=RenewablesUse)) +
  geom_bar(position="dodge", stat="count") +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 40)) +
  theme(text = element_text(size=13.75)) +
  xlab("Percieved impact on renewables use") +
  ylab("Count") +
  scale_fill_brewer(palette="RdYlGn")

cdr$FossilFuelUse <- factor(cdr$FossilFuelUse, level=c("Greatly increase","Somewhat increase", "No effect","Somewhat decrease", "Greatly decrease", "Don’t know"))

ggplot(data=cdr, aes(x=FossilFuelUse, y=..count.., fill=FossilFuelUse)) +
  geom_bar(position="dodge", stat="count") +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 40)) +
  theme(text = element_text(size=13.75)) +
  xlab("Percieved impact on fossil fuel use") +
  ylab("Count") +
  scale_fill_brewer(palette="RdYlGn")


cdr_fund_own <- cdr %>% dplyr::select(GovernmentFundingOwnership)

cdr_fund_own <- cdr_fund_own %>%
  mutate(splitColumn = str_split(GovernmentFundingOwnership, ",(?=T)", simplify = FALSE)) %>%
  unnest(splitColumn) %>%
  dplyr::select(-GovernmentFundingOwnership) %>%
  rename(GovernmentFundingOwnership = splitColumn)


cdr_fund_own$GovernmentFundingOwnership <- factor(
  cdr_fund_own$GovernmentFundingOwnership,level=c("The government should not provide any funding for CDR",
                                                  "The government should provide some of the funding for CDR, but projects should be owned and operated by fossil fuel companies",
                                                  "The government should provide some of the funding for CDR, but projects should be owned and operated by carbon dioxide removal companies",
                                                  "The government should provide some of the funding for CDR, but projects should be owned and operated by local communities" ,
                                                  "The government should provide some of the funding for CDR, and projects should be owned and operated by the government",
                                                  "The government should provide all the funding for CDR, but projects should be owned and operated by fossil fuel companies",
                                                  "The government should provide all the funding for CDR, but projects should be owned and operated by carbon dioxide removal companies",
                                                  "The government should provide all the funding for CDR, but projects should be owned and operated by local communities",
                                                  "The government should provide all the funding for CDR, and projects should be owned and operated by the government"
  ))

ggplot(data=cdr_fund_own, aes(x=GovernmentFundingOwnership, y=..count.., fill=GovernmentFundingOwnership)) +
  geom_bar(position="dodge", stat="count") +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 20)) +
  theme(text = element_text(size=13.75)) +
  xlab("Role of government in funding and ownership") +
  ylab("Count") +
  theme(legend.position = "none")

cdr_govt_funding <- cdr %>% filter(!is.na(GovernmentFundingMechanism))

ggplot(data=cdr_govt_funding, aes(x=GovernmentFundingMechanism, y=..count.., fill=GovernmentFundingMechanism)) +
  geom_bar(position="dodge", stat="count") +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 25)) +
  theme(text = element_text(size=13.75)) +
  xlab("Government funding & ownership") +
  ylab("Count") +
  theme(legend.position = "none")


#Ownership preferences when government is paying for all or part of CDR

cdr_fund_yes_own <- cdr_fund_own %>% filter(GovernmentFundingOwnership!="The government should not provide any funding for CDR")

cdr_fund_yes_own$GovtFund_Owner[cdr_fund_yes_own$GovernmentFundingOwnership == "The government should provide all the funding for CDR, and projects should be owned and operated by the government"] <- "Government"
cdr_fund_yes_own$GovtFund_Owner[cdr_fund_yes_own$GovernmentFundingOwnership == "The government should provide all the funding for CDR, but projects should be owned and operated by carbon dioxide removal companies"] <- "CDR companies"
cdr_fund_yes_own$GovtFund_Owner[cdr_fund_yes_own$GovernmentFundingOwnership == "The government should provide all the funding for CDR, but projects should be owned and operated by local communities"] <- "Communities"
cdr_fund_yes_own$GovtFund_Owner[cdr_fund_yes_own$GovernmentFundingOwnership =="The government should provide all the funding for CDR, but projects should be owned and operated by fossil fuel companies"] <- "Fossil fuel companies"

cdr_fund_yes_own$GovtFund_Owner[cdr_fund_yes_own$GovernmentFundingOwnership == "The government should provide some of the funding for CDR, but projects should be owned and operated by carbon dioxide removal companies"] <- "CDR companies"
cdr_fund_yes_own$GovtFund_Owner[cdr_fund_yes_own$GovernmentFundingOwnership =="The government should provide some of the funding for CDR, but projects should be owned and operated by local communities" ] <- "Communities"
cdr_fund_yes_own$GovtFund_Owner[cdr_fund_yes_own$GovernmentFundingOwnership == "The government should provide some of the funding for CDR, but projects should be owned and operated by fossil fuel companies"] <- "Fossil fuel companies"
cdr_fund_yes_own$GovtFund_Owner[cdr_fund_yes_own$GovernmentFundingOwnership == "The government should provide some of the funding for CDR, and projects should be owned and operated by the government"] <- "Government"

ggplot(data=cdr_fund_yes_own, aes(x=GovtFund_Owner, y=..count.., fill=GovtFund_Owner)) +
  geom_bar(position="dodge", stat="count") +
  scale_x_discrete(labels = function(x) str_wrap(x, width = 25)) +
  theme(text = element_text(size=13.75)) +
  xlab("Owner of government-funded projects") +
  ylab("Count") +
  theme(legend.position = "none")

table(cdr_fund_yes_own$GovtFund_Owner)/length(cdr_fund_yes_own$GovtFund_Owner)











cdr_gg <- cdr %>% dplyr::select(FossilFuelRole_Nationalization, FossilFuelRole_Untrustworthy, FossilFuelRole_Experience, FossilFuelRole_ProvideEnergy, weight_standard) %>% pivot_longer(cols = c(FossilFuelRole_Nationalization, FossilFuelRole_Untrustworthy, FossilFuelRole_Experience, FossilFuelRole_ProvideEnergy),names_to = "category", values_to = "responses")


cdr_gg$category[cdr_gg$category == 'FossilFuelRole_Nationalization'] <- "Should nationalize"
cdr_gg$category[cdr_gg$category == 'FossilFuelRole_Untrustworthy'] <- "Is untrustworthy"
cdr_gg$category[cdr_gg$category == 'FossilFuelRole_Experience'] <- "Brings experience"
cdr_gg$category[cdr_gg$category == 'FossilFuelRole_ProvideEnergy'] <- "Provided energy"

cdr_gg_count <- as.data.table(cdr_gg)
cdr_gg_count$responses <- factor(cdr_gg_count$responses,
                                 levels = c("Strongly approve",
                                            "Somewhat approve",
                                            "Somewhat disapprove",
                                            "Strongly disapprove",
                                            "Haven’t heard enough to say"))


ggplot(cdr_gg_count,
       aes(x = category, fill = responses)) +
  geom_bar(stat = "count", position = "dodge")+
  scale_fill_manual(values=c("#FF9000", "#FFC000", "#99CCFF", "#0033FF","#CCCCCC"))

